package iotwisp.school.system.service;

import java.util.HashMap;
import java.util.Map;

import iotwisp.school.system.qo.SmsValidSagaQO;
import iotwisp.base.cqrs.BusinessException;
import iotwisp.base.orm.BaseDao;
import iotwisp.base.util.RandomUtil;
import org.hibernate.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import iotwisp.school.system.command.SendValidSmsSCmd;
import iotwisp.school.system.entity.SmsValidSaga;

@Service
@Transactional
public class SmsValidSagaService extends BaseDao<SmsValidSaga, SmsValidSagaQO> {

	@Autowired
	private AliSmsSendService aliSmsSendService;

	public SmsValidSaga sendValidSms(SendValidSmsSCmd cmd) throws BusinessException {

		String code = RandomUtil.getNumber(0, 999999, 6);
		Map<String, String> params = new HashMap<String, String>();
		switch (cmd.getBizType()) {
		case "register":
			params.put("code", code);
			break;

		default:
			break;
		}

		aliSmsSendService.send(cmd.getMobile(), cmd.getBizType(), params);

		SmsValidSaga saga = new SmsValidSaga();
		saga.create(cmd, code);
		save(saga);

		return saga;
	}

	@Override
	protected Criteria buildCriteria(Criteria criteria, SmsValidSagaQO qo) {
		return criteria;
	}

	@Override
	protected Class<SmsValidSaga> getEntityClass() {
		return SmsValidSaga.class;
	}

}
